package com.zzxx.photograph.dao.impl;

import com.zzxx.photograph.dao.CommentDao;
import com.zzxx.photograph.domain.Comment;
import com.zzxx.photograph.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class CommentDaoImpl implements CommentDao {
    private JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public List<Comment> loadComment(int iid) {
        String sql = "select * from comment where iid=?";

        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Comment.class),iid);
    }

    @Override
    public void addComment(int iid, String text, int uid,String time) {
        String sql = "insert into comment (iid,text,uid,time) values (?,?,?,?)";
        jdbcTemplate.update(sql,iid,text,uid,time);
    }

    @Override
    public void deleteComment(int comid) {
        String sql = "delete from comment where comid = ?";
        jdbcTemplate.update(sql,comid);
    }

    @Override
    public Integer countComment(int iid) {
        String sql = "select count(*) from comment where iid = ?";
        return jdbcTemplate.queryForObject(sql,Integer.class,iid);
    }

    @Override
    public int addSupport(int comid) {
        String sql = "update comment set support=support+1 where comid = ? ";
        jdbcTemplate.update(sql,comid);
        String sql1 = "select support from comment where comid = ?";
        return jdbcTemplate.queryForObject(sql1,Integer.class,comid);
    }

    @Override
    public int addNonSupport(int comid) {
        String sql = "update comment set nonsupport=nonsupport+1 where comid = ? ";
        jdbcTemplate.update(sql,comid);
        String sql1 = "select nonsupport from comment where comid = ?";
        return jdbcTemplate.queryForObject(sql1,Integer.class,comid);
    }

}
